<template>
	<view>
		<view class="confirm-order">
      <view class="item row-between">
        <view style="width:150rpx;">订单编号</view>
        <view class="flex row" style="width:100%">
          <u-input v-model="orderSn" class="flex1 ml20 bg-gray" :border="true" :clearable="false"
                   placeholder="请输入订单编号" 
                   :custom-style="{ borderRadius: '80rpx' }" 
                   @blur="onOrderSnBlur" />
          <button class="btn br60 white bg-primary ml20" size="mini" @tap="scanCode">扫码</button>
        </view>
      </view>
      <view v-if="msgFalse==false" class="item row-between" style="height:40rpx;color:red;justify-content: center;padding-bottom: 20rpx; margin: 20rpx 0;">
        {{ msg }}
      </view>
      <view v-if="msgFalse==true" class="item row-between" style="height:40rpx;color:#4EF831FF;justify-content: center;padding-bottom: 20rpx; margin: 20rpx 0;">
        {{ msg }}
      </view>
      <view class="item row-between" style="justify-content: center;padding-bottom: 20rpx; margin: 20rpx 0;">
        <button class="btn bg-primary white" style="width: 90%; " @click="onSubmitOrder">核销</button>
      </view>



		</view>

		<loading-view v-if="showLoading" background-color="white" :size="50" />




	</view>
</template>

<script>

  import {
    getScoreGoodsDetailBySn,confirmHexiao


  } from '@/api/scoregoods.js'
	import {
		mapGetters
	} from 'vuex'


	export default {
		data() {
			return {
        msgFalse:true,
        msg:'',
        orderSn: '', // 添加订单编号数据
        goodsDetail: {}, // 商品详情数据
        showLoading: false // 加载状态
			};
		},

		computed: {

		},

		methods: {
      // 输入框失去焦点时调用
      onOrderSnBlur() {
        // 检查订单编号是否为空
        if (this.orderSn && this.orderSn.trim() !== '') {
          // 调用获取商品详情的方法
          this.getGoodsDetailFun();
        }
      },

      // 扫码功能
      scanCode() {
        uni.scanCode({
          success: (res) => {
            console.log('扫码成功', res);
            this.orderSn = res.result;
            // 扫码后自动获取商品详情
            this.getGoodsDetailFun();
          },
          fail: (err) => {
            console.log('扫码失败', err);
            if (err.errMsg) {
              uni.showToast({
                title: '扫码失败，请重试',
                icon: 'none'
              });
            }
          }
        });
      },

      // 获取商品信息
      getGoodsDetailFun() {
        // 显示加载状态
        this.showLoading = true;

        getScoreGoodsDetailBySn({
          order_sn: this.orderSn,

        }).then(res => {
          // 隐藏加载状态
          this.showLoading = false;
          
          if (res.code == 1) {
            const data = res.data;
            this.goodsDetail = data;

          } else {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            });
          }
        }).catch(err => {
          // 隐藏加载状态
          this.showLoading = false;
          console.error('获取商品详情失败', err);
          uni.showToast({
            title: '获取商品详情失败',
            icon: 'none'
          });
        });

      },

			// 点击下单
			onSubmitOrder() {
				// if (!this.isAgreeProtocol)
				// 	return uni.showToast({
				// 		title: '请先阅读并同意协议',
				// 		icon: 'none'
				// 	});
        this.msg='';
        this.msgFalse=true;

				if (!this.orderSn)
					return uni.showToast({
						title: '请输入订单码',
						icon: 'none'
					});

				uni.showModal({
					title: '温馨提示',
					content: '是否确认核销?',
					confirmColor: '#FF2C3C',
					success: async res => {
						if (!res.confirm) return;

						// #ifdef MP-WEIXIN
						// await this.getAuthMsg();
						// #endif


						this.orderBuyFun()

					}
				});
			},

			// 下单处理
			orderBuyFun() {


      const data={
        order_sn:this.orderSn,
      }
        confirmHexiao(data).then(res => {
        this.showLoading = false;
        this.msg=res.msg;
        if (res.code != 1) {
          this.msgFalse=false;

        }else{
          this.msgFalse=true;
        }

      });

			},

		},


		onLoad(options) {

		},

		onShow() {

		},



	}
</script>
<style lang="scss">
	.contain {
		background-color:#fff;
		border-radius: 24rpx;
		margin-top: 20rpx;
	}
  .confirm-order{
    background:#fff;
    padding:0 20rpx;
    box-sizing: border-box;
  }
</style>